<!--
 * @Author: albert shyttsyj@126.com
 * @Date: 2024-06-15 17:19:48
 * @LastEditors: albert shyttsyj@126.com
 * @LastEditTime: 2024-06-19 13:41:15
 * @FilePath: /sales-assistant-admin/src/views/authRedirect.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script>
import { wxAuthLogin, wxScanLogin } from "@/api/login";
import { getQueryValue } from "@/utils/index";
import { setToken, setTenantId, devicePathConvert } from "@/utils/auth";
import { getTenantEnable } from "@/utils/iyaobao";

export default {
  data() {
    return {
      isWeixin: navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1,
      tenantEnable: true,
      tenantId: "",
    };
  },

  async created() {
    let auth_code = getQueryValue("code");
    try {
      let { data } = this.isWeixin ? await wxAuthLogin(auth_code) : await wxScanLogin(auth_code);
      setToken(data);
      if (getTenantEnable()) {
        setTenantId(data.tenantId);
      }

      // 用以重置浏览器回调的url
      history.replaceState({}, "page", process.env.PUBLIC_PATH || "/");
      const path = await devicePathConvert(this.$route.query.redirectUrl || '');
      this.$router.replace(path || '/');
    } catch (error) {
      alert(error)
      console.log(error);
    }
  },
};
</script>

<template>
  <div></div>
</template>
